# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.
#
# from AUR - adjusted to our style
#
# Maintainer: Ali Molaei <ali dot molaei at protonmail dot com>
# Maintainer: Rafael Fontenelle <rafaelff at gnome dot org>

pkgname=mongodb
pkgver=8.2.1
_basever=8.2
_basedist=noble
pkgrel=2
pkgdesc='A high-performance, open source, schema-free document-oriented database.'
arch=('x86_64' 'aarch64')
url='https://www.mongodb.com/try/download/community'
license=('SSPL-1.0')
depends=('mongosh' 'curl' 'openssl')
makedepends=('chrpath')
optdepends=("mongodb-tools: The MongoDB tools provide import, export, and diagnostic capabilities.")
provides=("mongodb=$pkgver")
backup=("etc/mongodb.conf")

_repo_url=https://repo.mongodb.org/apt/ubuntu/dists/${_basedist}/mongodb-org/${_basever}/multiverse
source=(
	"$pkgname.sysusers"
	"$pkgname.tmpfiles"
	'LICENSE'
)
source_x86_64=(
	"$pkgname-org-server_${pkgver}_x86_64.deb"::"$_repo_url/binary-amd64/mongodb-org-server_${pkgver}_amd64.deb"
	"$pkgname-org-mongos_${pkgver}_x86_64.deb"::"$_repo_url/binary-amd64/mongodb-org-mongos_${pkgver}_amd64.deb"
)
source_aarch64=(
	"mongodb-org-server_${pkgver}_aarch64.deb"::"$_repo_url/binary-arm64/mongodb-org-server_${pkgver}_arm64.deb"
	"mongodb-org-mongos_${pkgver}_aarch64.deb"::"$_repo_url/binary-arm64/mongodb-org-mongos_${pkgver}_arm64.deb"
)
noextract=(
	"$pkgname-org-server_${pkgver}_${CARCH}.deb"
	"$pkgname-org-mongos_${pkgver}_${CARCH}.deb"
)

sha512sums=('889425b864c58a767aa5865c0ce9817361ad99fec78050fa600f14eaef5a56ce0bc41a03878233e99f4862596a94dafcfebebecd4d57443b742117b873ab813d'
            'a931c401792f4e7928e4778d91626c1ecc3e97e5728549b170c050de487b2e5234747b0ee2d5acc3d63b798716758c17e30914dcaa9a92ac386db39f8a45a05c'
            '05cc1d1ae1af4fd0f51dcf1a5fca9ee8124306eb1a5a4f3c9b17ec7376e9b58cfb38de97c3d82b549f48113f9a50cf98e1e25898f430ce23e6e884efac7deda3')
sha512sums_x86_64=('6f31b98578d381bb95260c04c654845dbcf5d9393953adf0700b32968749e25bf399330258eede0a7aa7d0494120a13276139da5e057e72c7148936b6cf359f8'
                   'f53b9aa49ec61485c8ae44d00b647eeaa0307e0c03f5d5f81cef3611246735203a944c6f73fbfe4d43731f0e4502d46e8e710d7faa8985a5902c6d3a7dc8f667')
sha512sums_aarch64=('dfda2698972656b77b2671377489db4f1bbe36595e6e5c9c298ba23d212e0f043f35334bc7dcd5f4c9097d070bc1e7cf2e02efcff4459fbdfc91d2270a4ca41d'
                    '7f4ee24b1e575950340013054ecfee2474ee5ae75a6e29132e0086f8df864a2f4634882449e42331daa119d4f9902dfcc7f5178b03f7794d7d2b6ff28d92bcef')
prepare() {
	mkdir -p output
	bsdtar -O -xf "$pkgname-org-server_${pkgver}_${CARCH}.deb" data.tar.zst |
    bsdtar -C output -xJf - #server extracted
	bsdtar -O -xf "$pkgname-org-mongos_${pkgver}_${CARCH}.deb" data.tar.zst |
    bsdtar -C output -xJf - #mongos extracted

	# Remove insecure RUNPATH '$ORIGIN/../lib' as reported by namcap
	chrpath -d output/usr/bin/mongos

	# Keep historical Arch dbPath
	sed -i 's|dbPath: /var/lib/mongo$|dbPath: /var/lib/mongodb|' \
    output/etc/mongod.conf

	# Keep historical Arch conf file name
	sed -i 's|/etc/mongod.conf|/etc/mongodb.conf|' \
    output/usr/lib/systemd/system/mongod.service

	# Keep historical Arch user name (no need for separate daemon group name)
	sed -i 's/User=mongod$/User=mongodb/' \
    output/usr/lib/systemd/system/mongod.service
	sed -i 's/Group=mongod$/Group=mongodb/' \
    output/usr/lib/systemd/system/mongod.service

	# Avoid legacy PID filepath
	sed -i 's|/var/run/|/var/|' output/usr/lib/systemd/system/mongod.service

	# Remove sysconfig file, used by upstream's init.d script not used on Arch
	sed -i '/^EnvironmentFile=/d' output/usr/lib/systemd/system/mongod.service

	# Make systemd wait as long as it takes for MongoDB to start
  # If MongoDB needs a long time to start, prevent systemd from restarting it
  # every 90 seconds. See: https://jira.mongodb.org/browse/SERVER-38086
	sed -i 's/\[Service]/[Service]\nTimeoutStartSec=infinity/' \
    output/usr/lib/systemd/system/mongod.service
}

package() {
	install -Dm 644 output/etc/mongod.conf "$pkgdir/etc/$pkgname.conf"
	install -Dm 644 output/usr/lib/systemd/system/mongod.service \
    "$pkgdir/usr/lib/systemd/system/$pkgname.service"
	install -Dm 755 output/usr/bin/* -t "$pkgdir/usr/bin"
	install -Dm 644 output/usr/share/man/man1/* -t "$pkgdir/usr/share/man/man1"
	install -Dm 644 "$pkgname.sysusers" "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
	install -Dm 644 "$pkgname.tmpfiles" "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
	install -Dm 644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

